function y = SEM(x,dim)
% calculates the standard error of the mean

if nargin==1, 
  % Determine which dimension SEM will use
  dim = find(size(x)~=1, 1 );
  if isempty(dim), dim = 1; end

  y = std(x)/sqrt(size(x,dim));
else
  y = std(x,0,dim)/sqrt(size(x,dim));
end